layui.use([ 'form','layer','jquery','table','laydate'], function() {
    var layer = layui.layer, $ = layui.jquery, form = layui.form,table=layui.table,laydate = layui.laydate;


    table.render({
        id:'CarInfoList'
        ,elem: '#CarInfoList'
        ,url: ctx+'/CarInfo/getAllCarInfoList'// 数据接口
        ,limit:10// 每页默认数
        ,limits:[10,20,30,40]
        ,cols: [[ // 表头
            {field:'carId', title: '车辆ID', align: 'center',width:160}
            ,{field:'number', title: '车牌号' , align: 'center',templet : '#sexTpl',width:160}
            ,{field:'teamId', title: '所属车队' , align: 'center',width:170}
            ,{field:'type', title: '车辆类型', align: 'center' ,width:130}
            ,{field:'seatnum', title: '座号', align: 'center' ,width:130}
            ,{field:'busMasterId', title: '车辆负责人' , align: 'center',width:200}
            ,{field:'status', title: '状态', width:80, templet : '#statusTpl',width:95, align: 'center'}
            ,{field:'note', title: '备注', align: 'center' }
            , {field: 'right', title: '操作', align: 'center', toolbar: "#barDemo",width:150}
        ]]
        ,page: true // 开启分页
        ,loading:true
        ,where: {timestamp: (new Date()).valueOf()}
    });


    table.on('tool(CarInfoList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {
            layer.confirm('确定要删除行么？', function (index) {
                $.ajax({
                    url : ctx + '/CarInfo/deleteCarInfoById/' + data.id,
                    type : "get",
                    success : function(d) {
                        if (d.code == 0) {
                            obj.del();
                        } else {
                            layer.msg("权限不足！", {
                                icon : 5
                            });
                        }
                    }
                })
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type : 2,
                title : "编辑车辆信息",
                area : [ '400px', '500px' ],
                content : ctx + "/CarInfo/editCarInfo/" + data.carId
            })
        }
    });

    form.on("submit(updateCarInfo)",function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.2});
        var index1 = parent.layer.getFrameIndex(window.name);
        var msg,flag=false;
        $.ajax({
            type: "post",
            async:false,
            url: ctx+"/CarInfo/updateCarInfo",
            data:data.field,
            dataType:"json",
            success:function(d){
                if(d.code==0){
                    msg="车辆信息更新成功！";
                    flag=true;
                }else{
                    msg="车辆信息更新失败！";
                }
            },
            error:function(){
                layer.msg("车辆信息更新失败！",{icon:2});
                layer.closeAll();
                return false;
            }

        });
        setTimeout(function () {
            parent.layer.close(index1);
            top.layer.close(index);
            if(flag){
                top.layer.msg(msg,{icon: 1});
            }else{
                top.layer.msg(msg,{icon: 5});
            }
            parent.location.reload();
        }, 2000);
        return false;
    })
});


